/**
 * 
 */
package ro.fortech.peaa.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ro.fortech.peaa.domain.Employee;
import ro.fortech.peaa.service.api.IEmployeeIdentityMap;

/**
 * Implementation for employee IdentityMap
 * @author robert
 *
 */
public class EmployeeIdentityMap implements IEmployeeIdentityMap {
	
	private Map<Integer, Employee> map;
	
	protected Log logger = LogFactory.getLog(getClass());
	
	public void setMap(Map<Integer, Employee> map) {
		this.map = map;
	}

	/* (non-Javadoc)
	 * @see ro.fortech.peaa.repository.api.IEmployeeIdentityMap#get(java.lang.Integer)
	 */
	public Employee get(Integer id) throws Exception {
		logger.info("get employee with id " + id + " from Identity Map");
		Employee emp = null;
		emp = map.get((Integer) id);
		return emp;
	}

	/* (non-Javadoc)
	 * @see ro.fortech.peaa.repository.api.IEmployeeIdentityMap#put(ro.fortech.peaa.domain.Employee)
	 */
	public void put(Employee emp) throws Exception {
		logger.info("put employee with id " + emp.getId() + " to Identity Map");
		map.put(emp.getId(), emp);
	}

	/*
	 * (non-Javadoc)
	 * @see ro.fortech.peaa.service.api.IEmployeeIdentityMap#remove(ro.fortech.peaa.domain.Employee)
	 */
	public void remove(Employee emp) throws Exception {
		logger.info("remove employee with id " + emp.getId() + " from Identity Map");
		map.remove(emp.getId());
	}

	@Override
	public String toString() {
		String result = "";
		Iterator<Employee> it = map.values().iterator();
		result += "Employee Identity Map\nID\tFirstName\tLastname\n";
		while (it.hasNext()) {
			Employee emp = it.next();
			result += emp.getId() + "\t" + emp.getFirstName() + "\t" + emp.getLastName() + "\n";
		}
		return result;	
	}

	@Override
	public Integer size() throws Exception {
		return map.size();
	}

	@Override
	public List<Employee> getAll() throws Exception {
		List<Employee> employees = new ArrayList<Employee>(map.values());
		return employees;
	}
}
